package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.BaseEntity;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPageParamDto;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingProjectPlan;
import com.zzyl.mapper.NursingLevelMapper;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NursingPlanServiceImpl implements NursingPlanService {

    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    @Autowired
    private NursingLevelMapper nursingLevelMapper;

    @Override
    public List<NursingPlanVo> getAll() {
        List<NursingPlan> list=nursingPlanMapper.selectAll();
        List<NursingPlanVo> nursingPlanVos = BeanUtil.copyToList(list, NursingPlanVo.class);
        return nursingPlanVos;
    }

    @Override
    public void add(NursingPlanDto nursingPlanDto) {
        NursingPlan bean = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        nursingPlanMapper.add(bean);

        List<NursingProjectPlan> projectPlans = bean.getProjectPlans();

        for (NursingProjectPlan projectPlan : projectPlans) {
            projectPlan.setPlanId(bean.getId());
        }
        nursingProjectPlanMapper.add(projectPlans);

    }

    @Override
    public PageResponse<NursingPlanVo> get(NursingPageParamDto nursingPageParamDto) {
        PageHelper.startPage(nursingPageParamDto.getPageNum(),nursingPageParamDto.getPageSize());
        List<NursingPlan> nursingPlans= nursingPlanMapper.get(nursingPageParamDto);
        Page<NursingPlan> nursingPlans1 = (Page<NursingPlan>) nursingPlans;


        return PageResponse.of(nursingPlans1, NursingPlanVo.class) ;
    }

    @Override
    public NursingPlanVo getById(Integer id) {

        NursingPlan nursingPlan = nursingPlanMapper.selectById(id);
        List<NursingProjectPlan> nursingProjectPlans=nursingProjectPlanMapper.getById(id);
        nursingPlan.setProjectPlans(nursingProjectPlans);
        NursingPlanVo bean = BeanUtil.toBean(nursingPlan, NursingPlanVo.class);
        return bean;
    }

    @Override
    public void updateById(NursingPlanDto nursingPlanDto) {
        NursingPlan bean = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);

        nursingPlanMapper.update(bean);
        List<NursingProjectPlan> projectPlans = bean.getProjectPlans();
        for (NursingProjectPlan projectPlan : projectPlans) {
            projectPlan.setPlanId(bean.getId());
        }
        nursingProjectPlanMapper.delete(bean.getId());
        nursingProjectPlanMapper.add(projectPlans);
    }

    @Override
    public void delete(Integer id) {
        nursingProjectPlanMapper.delete((long)id);
        nursingPlanMapper.deleteById(id);

    }

    @Override
    public void putStatus(Integer id, Integer status) {
        nursingPlanMapper.putStatus(id,status);
    }
}
